package com.longrise.android.byjk.plugins.hra;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.hra.hratabfirst.HraOrderFragment;
import com.longrise.android.byjk.plugins.hra.hratabsecond.MineAppointFragment;
import com.longrise.android.byjk.plugins.hra.hratabthird.MineEndorseFragment;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.PrintLog;

/**
 * Created by Administrator on 2018/1/3.
 * HRA预约模块主activity
 */

public class HraMainActivity extends BaseActivity2 implements TabLayout.OnTabSelectedListener {

    //Tab 文字
    private final int[] mTab_titles = new int[]{R.string.hra_order, R.string.mine_order, R.string.mine_endorse};
    //Tab 图片
    private final int[] mTab_images = new int[]{R.drawable.selector_hra_order,
            R.drawable.selector_mine_order, R.drawable.selector_mine_endorse};

    private TabLayout mTb;
    private static final String ISFIRST = "isfirst";
    private long mExitTime;
    private FrameLayout mFl;
    private Bundle mBundle;
    private HraOrderFragment mHraOrderFragment;
    private MineAppointFragment mSecondFragment;
    private MineEndorseFragment mEndorseFragment;
    private FragmentManager mSupportFragmentManager;
    private int flags;
    private int mSelectPage = 0;


    public static void pushActivity(Activity activity, int page) {
        Intent intent = new Intent(activity, HraMainActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("page", page);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    public void getIntentDatas() {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            mSelectPage = extras.getInt("page");
        }
    }

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        mBundle = savedInstanceState;
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        mFl = (FrameLayout) findViewById(R.id.main_container);
        mTb = (TabLayout) findViewById(R.id.main_tablayout);
        setToolbarVisible(false);
        initFragment();
        setTabs();
        initAdapter();
        getIntentDatas();
        choosePage();
    }

    @Override
    public void onToolbarBackClick() {

    }

    /**
     * 默认选择的页面
     */
    private void choosePage() {
        TabLayout.Tab tabAt = mTb.getTabAt(mSelectPage);
        if (tabAt != null) {
            tabAt.select();
        }
    }

    /**
     * 初始化fragment
     */
    private void initFragment() {
        mSupportFragmentManager = getSupportFragmentManager();
        if (mBundle != null) {
            mHraOrderFragment = (HraOrderFragment) mSupportFragmentManager.findFragmentByTag(HraOrderFragment.class.getName());
            mSecondFragment = (MineAppointFragment) mSupportFragmentManager.findFragmentByTag(MineAppointFragment.class.getName());
            mEndorseFragment = (MineEndorseFragment) mSupportFragmentManager.findFragmentByTag(MineEndorseFragment.class.getName());
            mSupportFragmentManager.beginTransaction()
                    .show(mHraOrderFragment)
                    .hide(mSecondFragment)
                    .hide(mEndorseFragment)
                    .commit();
        } else {
            mHraOrderFragment = new HraOrderFragment();
            mSecondFragment = new MineAppointFragment();
            mEndorseFragment = new MineEndorseFragment();
            mSupportFragmentManager.beginTransaction()
                    .add(R.id.main_container, mHraOrderFragment, HraOrderFragment.class.getName())
                    .add(R.id.main_container, mSecondFragment, MineAppointFragment.class.getName())
                    .add(R.id.main_container, mEndorseFragment, MineEndorseFragment.class.getName())
                    .hide(mSecondFragment)
                    .hide(mEndorseFragment)
                    .commit();
        }
    }

    private void setTabs() {
        for (int i = 0; i < mTab_images.length; i++) {
            TabLayout.Tab tab = mTb.newTab();
            View tabview = View.inflate(this, R.layout.tab_main, null);
            tab.setCustomView(tabview);

            TextView tvTab = (TextView) tabview.findViewById(R.id.tv_tab);
            tvTab.setText(mTab_titles[i]);
            ImageView ivTab = (ImageView) tabview.findViewById(R.id.img_tab);
            ivTab.setImageResource(mTab_images[i]);
            mTb.addTab(tab);
        }
    }

    private void initAdapter() {

        mTb.addOnTabSelectedListener(this);
    }


    //--------------- 底部tab的选择事件 -----------------------
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        int position = tab.getPosition();
        switch (position) {
            case 0:
                mSupportFragmentManager.beginTransaction()
                        .show(mHraOrderFragment)
                        .hide(mSecondFragment)
                        .hide(mEndorseFragment)
                        .commit();
                break;
            case 1:
                mSupportFragmentManager.beginTransaction()
                        .show(mSecondFragment)
                        .hide(mHraOrderFragment)
                        .hide(mEndorseFragment)
                        .commit();
                break;
            case 2:
                mSupportFragmentManager.beginTransaction()
                        .show(mEndorseFragment)
                        .hide(mSecondFragment)
                        .hide(mHraOrderFragment)
                        .commit();
                break;
        }

    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
    //--------------------------------------------------------
}
